﻿/// <reference name="MicrosoftAjax.js"/>
/*
 * ----------------------------------------------------------- 
 * This script is distributed under the GNU Lesser General Public License. 
 * Read the entire license text here: http://www.gnu.org/licenses/lgpl.html
 */
Type.registerNamespace("AjaxControlExtenders");

AjaxControlExtenders.DateTimePickerBehavior = function(element) {
    AjaxControlExtenders.DateTimePickerBehavior.initializeBase(this, [element]);    
    this._clickHandler = null;
    this._buttonClickHandler = null;    
    this._align = null;    
    this._button = null;
    this._firstDayOfWeek = 0;    
    this._format = null;        
    this._popupButtonID = null;
    this._showsTime = true;    
    this._showsOtherMonths = false;
    this._singleClick = true;    
    this._timeMode = "12";
    this._weekNumbers = true;
}

AjaxControlExtenders.DateTimePickerBehavior.prototype = {
    initialize: function() {
        AjaxControlExtenders.DateTimePickerBehavior.callBaseMethod(this, 'initialize');
                                         
        if (this._button != null) {  
            this._buttonClickHandler = Function.createDelegate(this, this._onButtonClick);                  
            $addHandler(this._button, 'click', this._buttonClickHandler);
        }
        
        if (this._buttonClickHandler == null) {   
            this._clickHandler = Function.createDelegate(this, this._onClick);       
            $addHandler(this.get_element(), 'click', this._clickHandler);
        }                                                                                                   
    },
    get_align : function() {         
        return this._align; 
    },
    set_align : function(value) { 
        if (this._align != value) {
            this._align = value; 
            this.raisePropertyChanged("align");
        }
    },        
    get_button : function() {         
        return this._button; 
    },
    set_button : function(value) { 
        if (this._align != value) {
            this._button = value; 
            this.raisePropertyChanged("align");
        }
    },   
    get_firstDayOfWeek : function() {         
        return this._firstDayOfWeek; 
    },
    set_firstDayOfWeek : function(value) { 
        if (this._firstDayOfWeek != value) {
            this._firstDayOfWeek = value; 
            this.raisePropertyChanged("firstDayOfWeek");
        }
    }, 
    get_format : function() {         
        return this._format; 
    },
    set_format : function(value) { 
        if (this._format != value) {
            this._format = value; 
            this.raisePropertyChanged("format");
        }
    },        
    get_popupButtonID : function() {         
        return this._popupButtonID; 
    },
    set_popupButtonID : function(value) { 
        if (this._popupButtonID != value) {
            this._popupButtonID = value; 
            this.raisePropertyChanged("popupButtonID");
        }
    }, 
    get_showsTime : function() {         
        return this._showsTime; 
    },
    set_showsTime : function(value) { 
        if (this._showsTime != value) {
            this._showsTime = value; 
            this.raisePropertyChanged("showsTime");
        }
    },      
    get_showsOtherMonths : function() {         
        return this._showsOtherMonths; 
    },
    set_showsOtherMonths : function(value) { 
        if (this._showsOtherMonths != value) {
            this._showsOtherMonths = value; 
            this.raisePropertyChanged("showsOtherMonths");
        }
    },    
    get_singleClick : function() {         
        return this._singleClick; 
    },
    set_singleClick : function(value) { 
        if (this._singleClick != value) {
            this._singleClick = value; 
            this.raisePropertyChanged("singleClick");
        }
    },
    get_timeMode : function() {         
        return this._timeMode; 
    },
    set_timeMode : function(value) { 
        if (this._timeMode != value) {
            this._timeMode = value; 
            this.raisePropertyChanged("timeMode");
        }
    },    
    get_theme : function() {         
        return this._theme; 
    },    
    set_theme : function(value) { 
        if (this._theme != value) {
            this._theme = value; 
            this.raisePropertyChanged("theme");
        }
    },
    get_weekNumbers : function() {         
        return this._weekNumbers; 
    },
    
    set_weekNumbers : function(value) { 
        if (this._weekNumbers != value) {
            this._weekNumbers = value; 
            this.raisePropertyChanged("weekNumbers");
        }
    },
    show : function() {                    
        var params = {
            "inputField" : this.get_element(),
            "displayArea" : null,
            "button" : this._button,
            "eventName" : "click",
            "ifFormat" : this._format,
            "daFormat" : this._format,
            "singleClick" : this._singleClick,
            "disableFunc" : null,
            "dateStatusFunc" : null,
            "dateText" : null,
            "firstDay" : this._firstDayOfWeek,
            "align" : this._align,
            "range" : [1900, 2070],
            "weekNumbers" : this._weekNumbers,
            "flat" : null,
            "flatCallback" : null,
            "onSelect" : this._onSelected,
            "onClose" : this._onClose,
            "onUpdate" : null,
            "date" : null,
            "showsTime" : this._showsTime,
            "timeFormat" : this._timeMode,
            "electric" : true,
            "step" : 2,
            "position" : null,
            "cache" : false,
            "showOthers" : this._showsOtherMonths,
            "multiple" :null            
        }; 
                                
        var dateEl = params.inputField || params.displayArea;
        var dateFmt = params.inputField ? params.ifFormat : params.daFormat;
        var mustCreate = false;
        var cal = window.calendar;
        if (dateEl)
            params.date = Date.parseDate(dateEl.value || dateEl.innerHTML, dateFmt);
        if (!(cal && params.cache)) {
            window.calendar = cal = new Calendar(params.firstDay, params.date, params.onSelect, params.onClose);
            cal.showsTime = params.showsTime;
            cal.time24 = (params.timeFormat == "24");
            cal.weekNumbers = params.weekNumbers;
            mustCreate = true;
        } else {
            if (params.date)
                cal.setDate(params.date);
            cal.hide();
        }
        if (params.multiple) {
            cal.multiple = {};
            for (var i = params.multiple.length; --i >= 0;) {
                var d = params.multiple[i];	
                var ds = d.print("%Y%m%d");	
                cal.multiple[ds] = d;
            }
        }
        cal.showsOtherMonths = params.showOthers;
        cal.yearStep = params.step;
        cal.setRange(params.range[0], params.range[1]);
        cal.params = params;
        cal.setDateStatusHandler(params.dateStatusFunc);
        cal.getDateText = params.dateText;
        cal.setDateFormat(dateFmt);
        if (mustCreate) 
            cal.create();        
        cal.refresh();
        if (!params.position)
            cal.showAtElement(params.button || params.displayArea || params.inputField, params.align);
        else
            cal.showAt(params.position[0], params.position[1]);            
    },
    _onClose : function(cal) {            
        cal.hide();
        //cal.destroy();        
    },
    _onButtonClick : function() {
        this.show();     
    },
    _onClick : function() {
        this.show();                        
    },
    _onSelected : function(cal) {
        var p = cal.params;                       
        var update = (cal.dateClicked || p.electric);
               
        if (update && p.inputField) {        
            p.inputField.value = cal.date.print(p.ifFormat);
            /*            
            if (typeof(p.inputField.onchange) == "function")
                p.inputField.onchange();
            */
        }
                
        if (update && p.displayArea)
            p.displayArea.innerHTML = cal.date.print(p.daFormat);
        if (update && typeof p.onUpdate == "function")
            p.onUpdate(cal);
                    
        if (update && p.flat) {
            if (typeof p.flatCallback == "function")
                p.flatCallback(cal);
        }
                
        if (update && p.singleClick && cal.dateClicked)
            cal.callCloseHandler();			
    },
    dispose: function() {                    
        var e = this.get_element();
        // Detach events                
        if (this._clickHandler)
        {                
            $removeHandler(e, 'click', this._clickHandler);
            this._clickHandler = null;
        }
        
        if (this._buttonClickHandler)
        {            
            $removeHandler(this._button, 'click', this._buttonClickHandler);
            this._buttonClickHandler = null;
        }
        AjaxControlExtenders.DateTimePickerBehavior.callBaseMethod(this, 'dispose');
    }
}
AjaxControlExtenders.DateTimePickerBehavior.registerClass('AjaxControlExtenders.DateTimePickerBehavior', Sys.UI.Behavior);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();